package com.jswlw.service.biz.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jswlw.dao.biz.DistributorAmountConsumeMapper;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.entity.biz.DistributorAmountConsume;
import com.jswlw.service.biz.DistributorAmountConsumeService;
import com.jswlw.service.biz.DistributorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Slf4j
@Service
public class DistributorAmountConsumeServiceImpl implements DistributorAmountConsumeService {

    @Autowired
    DistributorAmountConsumeMapper mapper;
    @Autowired
    DistributorService distributorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DistributorAmountConsume distributorAmountConsume) {
        mapper.insertSelective(distributorAmountConsume);
        Distributor distributor = distributorService.find4Id(distributorAmountConsume.getDistributorId());
        if (Objects.nonNull(distributor)) {
            if (Objects.nonNull(distributorAmountConsume.getLl())) {
                distributor.setLl(distributor.getLl() + distributorAmountConsume.getLl());
            }

            if (Objects.nonNull(distributorAmountConsume.getSc())) {
                distributor.setSc(distributor.getSc() + distributorAmountConsume.getSc());
            }
            distributorService.update(distributor);
        }
    }

    @Override
    public List<DistributorAmountConsume> queryList(DistributorAmountConsume filter) {
        return mapper.queryList(filter);
    }

    @Override
    public Page<DistributorAmountConsume> queryPage(DistributorAmountConsume filter) {
        PageHelper.startPage(filter.getOffset(), filter.getLimit());
        return (Page<DistributorAmountConsume>) mapper.queryList(filter);
    }
}
